package com.bidevalution.controller.api;

import com.bidevalution.business.context.FileType;
import com.bidevalution.business.service.ViewFileService;
import com.bidevalution.dao.entity.BeSectionEnterprise;
import com.bidevalution.dao.entity.FileInfo;
import com.bidevalution.support.dto.TreeNodeDTO;
import com.qtp.core.model.Response;
import com.qtp.core.util.GsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 查看招投标文件相关接口
 * @author huyiqi
 * @date 2019/12/7
 */
@RestController
@RequestMapping("/viewFile")
@Api(tags = "查看招投标文件")
public class ViewFileController extends BaseController{

    @ApiOperation(value = "查询附件类型", response = FileType.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionId", value = "标段id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "stepId", value = "阶段id", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "isZb", value = "是否招标：true查看招标文件，false查看投标文件", required = true, dataType = "Boolean", paramType = "query")})
    @GetMapping("/getFileTypes")
    Response<List<Map<String, Object>>> getFileTypes(@RequestParam("sectionId") String sectionId
            , @RequestParam("isZb") boolean isZb, @RequestParam(value = "stepId", required = false) String stepId) {
        try {
            List<FileType> list = viewFileService.getFileTypes(sectionId, stepId, isZb);
            List<Map<String, Object>> result = new ArrayList<>();
            if(list!=null && !list.isEmpty()) {
                for(FileType type:list) {
                    Map<String, Object> item = new HashMap<>(4);
                    item.put("name", type.getName());
                    item.put("code", type.getCode());
                    result.add(item);
                }
            }
            return Response.ok(result);
        } catch (Exception e) {
            log.error("无法获取附件类型（评审因素）");
            return Response.fail("无法获取附件类型（评审因素）");
        }
    }

    @ApiOperation(value = "查询附件结构", response = FileType.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionId", value = "标段id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "enterpriseId", value = "企业id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "fileTypeCode", value = "附件类型的code值", required = true, dataType = "Integer", paramType = "query")})
    @GetMapping("/getFileStructure")
    Response<List<TreeNodeDTO>> getFileStructure(@RequestParam("sectionId") String sectionId
            , @RequestParam("fileTypeCode") int fileTypeCode, @RequestParam(value = "enterpriseId",required = false) String enterpriseId) {
        try {
            FileType fileType = FileType.getByCode(fileTypeCode);
            return Response.ok(viewFileService.getFileStructure(sectionId, fileType, enterpriseId));
        } catch (Exception e) {
            log.error("无法附件结构");
            return Response.fail("无法附件结构");
        }
    }

    @ApiOperation(value = "查询附件列表", response = FileType.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "treeNodeId", value = "树节点的id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "enterpriseId", value = "企业id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "fileTypeCode", value = "附件类型的code值", required = true, dataType = "Integer", paramType = "query")})
    @GetMapping("/getFileList")
    Response<List<FileInfo>> getFileList(@RequestParam("treeNodeId") String treeNodeId
            , @RequestParam("fileTypeCode") int fileTypeCode, @RequestParam(value = "enterpriseId",required = false) String enterpriseId) {
        try {
            FileType fileType = FileType.getByCode(fileTypeCode);
            String[] ids = treeNodeId.split(",");
            String projectId = ids[0];
            String nodeId = ids[1];
            return Response.ok(viewFileService.getFileInfo(projectId, nodeId, enterpriseId, fileType));
        } catch (Exception e) {
            log.error("无法获取附件列表");
            return Response.fail("无法获取附件列表");
        }
    }

    @ApiOperation(value = "查询投标单位列表", response = FileType.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionId", value = "标段id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "stepId", value = "阶段id", required = false, dataType = "String", paramType = "query")})
    @GetMapping("/getEntList")
    Response<List<BeSectionEnterprise>> getEntList(@RequestParam("sectionId") String sectionId
            ,@RequestParam(value = "stepId", required = false) String stepId) {
        try {
            List<BeSectionEnterprise> list = viewFileService.getSectionEntList(sectionId, stepId);
            return Response.ok(list);
        } catch (Exception e) {
            log.error("无法获取投标单位列表", e);
            return Response.fail("无法获取投标单位列表");
        }
    }


    @Autowired
    private ViewFileService viewFileService;
}
